package com.starblink.android.basic.ext

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Point
import android.graphics.Rect
import android.view.View
import android.view.WindowManager
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.FragmentUtils
import com.starblink.android.basic.util.Ghost

/**
 * Activity相关
 */

//内联函数+标注泛型 = 泛型实例化

/**true表示Activity不可用*/
inline fun Activity.isNotAvailable() = this.isDestroyed || this.isFinishing

/**true表示Fragment所属Activity不可用*/
inline fun Fragment.isActivityNotAvailable(): Boolean {
    val activity = this.requireActivity()
    return activity.isDestroyed || activity.isFinishing
}

inline fun <reified T> Fragment.gotoActivity(
    flag: Int = -1,
    bundle: Array<out Pair<String, Any?>>? = null,
) {
    activity?.gotoActivity<T>(flag, bundle)
}

inline fun <reified T> View.gotoActivity(
    flag: Int = -1,
    bundle: Array<out Pair<String, Any?>>? = null,
) {
    context.gotoActivity<T>(flag, bundle)
}

inline fun <reified T> Context.gotoActivity(
    flag: Int = -1,
    bundle: Array<out Pair<String, Any?>>? = null,
) {
    val intent = Intent(this, T::class.java).apply {
        if (flag != -1) {
            this.addFlags(flag)
        }
        if (this !is Activity) {
            this.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
        if (bundle != null) {
            putExtras(bundle.toBundle()!!)
        }
    }
    startActivity(intent)
}


inline fun <reified T> Fragment.gotoActivityForResult(
    flag: Int = -1,
    bundle: Array<out Pair<String, Any?>>? = null,
    crossinline callback: ((result: Intent?) -> Unit),
) {
    activity?.gotoActivityForResult<T>(flag, bundle, callback)
}

inline fun <reified T> View.gotoActivityForResult(
    flag: Int = -1,
    bundle: Array<out Pair<String, Any?>>? = null,
    crossinline callback: ((result: Intent?) -> Unit),
) {
    (context as FragmentActivity).gotoActivityForResult<T>(flag, bundle, callback)
}

inline fun Context.gotoActivityForResult(
    clazz: Class<*>,
    flag: Int = -1,
    bundle: Array<out Pair<String, Any?>>? = null,
    crossinline callback: ((result: Intent?) -> Unit),
) {
    val intent = Intent(this, clazz).apply {
        if (flag != -1) {
            this.addFlags(flag)
        }
        if (bundle != null) {
            //调用自己的扩展方法-数组转Bundle
            putExtras(bundle.toBundle()!!)
        }
    }
    when (this) {
        is FragmentActivity -> {
            Ghost.launchActivityForResult(this, intent, callback)
        }

        is Activity -> {
            startActivity(intent)
        }

        else -> {
            startActivity(intent.apply {
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            })
        }
    }
}

//真正执行AcytivityForResult的方法，使用Ghost的方式执行
inline fun <reified T> FragmentActivity.gotoActivityForResult(
    flag: Int = -1,
    bundle: Array<out Pair<String, Any?>>? = null,
    crossinline callback: ((result: Intent?) -> Unit),
) {
    val intent = Intent(this, T::class.java).apply {
        if (flag != -1) {
            this.addFlags(flag)
        }
        if (bundle != null) {
            //调用自己的扩展方法-数组转Bundle
            putExtras(bundle.toBundle()!!)
        }
    }
    Ghost.launchActivityForResult(this, intent, callback)
}

//cls: Class<*>?,
//inline fun <reified T> Activity.checkLogin(
//    ctx: FragmentActivity,
//    vararg method: () -> Unit
//) {
//    var isLogin = UserDataCenter.isLogin()
//    if (isLogin) {
//        method
//    } else {
//
////        gotoActivityForResult<Demo10Activity>(bundle = arrayOf("id" to "123", "name" to "zhangsan")) {
////            val text = it?.getStringExtra("text")
////            toast("拿到返回数据：$text")
////        }
//
//        ctx.gotoActivityForResult<T> {
//            val text = it?.getStringExtra("text")
//            toast("拿到返回数据：$text")
//        }
//
//    }
//}

//fun <T> loadResource(resource: String?, parse: CCMMPParse<T>, clazz: Class<T>): T? {
//    val model = HJKitConfigAssembledResourceModel()
//    model.setContent(resource)
//    val result: T = parse.parse(model)
//    return if (clazz.isInstance(result)) {
//        clazz.cast(result)
//    } else null
//}


fun Activity.getRealNavBarHeight(): Int {
    val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val display = wm.defaultDisplay
    val realSize = Point()
    display.getRealSize(realSize)

    val contentView = window.decorView.findViewById<View>(android.R.id.content)
    val rect = Rect()
    contentView.getGlobalVisibleRect(rect)
    return realSize.y - rect.bottom
}


fun FragmentActivity.addShowFragment(
    fragment: Fragment,
    containerId: Int,
    hiddenFragment: Fragment? = null,
    tag: String? = null,
    add2Stack: Boolean = false,
    fadeAnim: Boolean = false,
) {
    FragmentUtils.add(
        supportFragmentManager,
        fragment,
        containerId,
        tag,
        add2Stack,
        if (fadeAnim) androidx.appcompat.R.anim.abc_fade_in else 0,
        if (fadeAnim) androidx.appcompat.R.anim.abc_fade_out else 0,
        0,
        0
    )
    if (hiddenFragment != null) {
        FragmentUtils.showHide(fragment, hiddenFragment)
    }
}